<?php

namespace App\Http\Controllers\Collect;

use App\Service\Collect\Eastmoney;


class EastmoneyController extends BaseController
{
    // 东方财富网采集
    public function initialize()
    {
        parent::initialize();
    }


    // 采集东风财富网股吧
    //   $url = 'https://so.eastmoney.com/tiezi/s?keyword='.$keyword;   // 东风财富网股吧
    //   $url = https://so.eastmoney.com/tiezi/s?keyword=易见股份;   // 东风财富网股吧
    public function tiezi()
    {
        $page    = 1;
        $size    = 10;
        $keyword = '易见股份';

        return $this->getAndSave($keyword, 'tiezi', $page, $size);
    }


    // 采集东风财富网资讯
    //   $url = 'https://so.eastmoney.com/news/s?keyword='.$keyword;   // 东风财富网资讯
    public function news()
    {
        $page    = 1;
        $size    = 10;
        $keyword = '易见股份';

        return $this->getAndSave($keyword, 'news', $page, $size);
    }


    // 采集东风财富网文章
    //   $url = 'https://so.eastmoney.com/carticle/s?keyword='.$keyword;    // 东风财富网文章
    public function carticle()
    {
        $page    = 1;
        $size    = 10;
        $keyword = '易见股份';

        return $this->getAndSave($keyword, 'carticle', $page, $size);
    }


    // 采集东风财富网公告
    //   $url = 'https://so.eastmoney.com/ann/s?keyword='.$keyword;      // 东风财富网公告
    public function ann()
    {
        $page    = 1;
        $size    = 10;
        $keyword = '易见股份';

        return $this->getAndSave($keyword, 'ann', $page, $size);
    }


    // 执行采集
    protected function getAndSave($keyword, $type, $page = 1, $size = 10)
    {
        $collect = new Eastmoney();
        $list    = $collect->getApiList($keyword, $type, $page, $size);
        if(empty($list)){
            if($collect->getCode() == 500){
                return $this->error($collect->getError());
            } else{
                return $this->success([], '没有采集数据！');
            }
        }

        // 展示
        $this->show($list[0]);

        // 采集详情内容
        //   TODO 这里采集的是百度的内容
        foreach($list as $key => $item){
            // 提示
            $this->showMsg(' '.($key + 1).'：==> 采集文章：'.$item['title']);

            $res = $collect->saveApiDataToDb($item, $key, $type);
            if($res === false){
                if($collect->getCode() == 500){
                    return $this->error($collect->getError());
                } else{
                    $this->showMsg($collect->getError());
                }
            }
        }

        return $this->success([], '采集完成');
    }

}


